package com.yyw.web;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yyw.bean.Dish;
import com.yyw.common.PageParam;
import com.yyw.common.R;
import com.yyw.dto.DishDto;
import com.yyw.service.DishService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping("/dish")
@RestController
public class DishController {
    @Autowired
    private DishService dishService;


    /**
     * 添加菜品
     * @param dishDto
     * @return
     */
    @PostMapping
    public R add(@RequestBody DishDto dishDto) {
        //
        int row = dishService.add(dishDto);

        //
        if (row > 0){
            return R.success("菜品添加成功！");
        }

        return R.error("菜品添加失败！");
    }


    /**
     * 菜品分页查询
     * @param pageParam
     * @return
     */
    @GetMapping("/page")
    public R findPage(PageParam pageParam){
        //
        IPage<DishDto> page = dishService.findPage(pageParam);

        //
        return R.success(page);
    }


    /**
     * 根据id查询菜品数据
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R findById(@PathVariable Long id) {
        DishDto dishDto = dishService.findById(id);

        return R.success(dishDto);

    }


    /**
     * 更新菜品
     * @param dishDto
     * @return
     */
    @PutMapping
    public R update(@RequestBody DishDto dishDto){
        int row = dishService.update(dishDto);

        if (row > 0){
            return R.success("菜品更新成功！");
        }
        return R.error("菜品更新失败！");
    }


    /**
     * 根据分类id查询菜品数据
     * @param categoryId
     * @return
     */
    @GetMapping("/list")
    public R findByCategoryId(Long categoryId, Integer status) {
        List<DishDto> dishList = dishService.findByCategoryId(categoryId, status);

        return R.success(dishList);
    }
}
